﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
    public class AdvertiseRefDAO//:BaseDAO<RELAXEntities,RelaxAdvertise>
   {
       #region value
       private readonly RelaxDataContext _context=new RelaxDataContext();
       #endregion

       #region Hàm lấy dữ liệu

       public IQueryable<RelaxAdvertiseDataContract> GetData()
       {
           var lReturn = (from a in _context.ADVERTISE_REFs
               join v in _context.ADVERTISE_VALUEs on a.id equals v.advertiseid
               select new RelaxAdvertiseDataContract
               {
                   Id = Convert.ToInt32(a.id),
                   Name = v.name,
                   Image = v.image,
                   Link = v.link,
                   Target = v.target,
                   Decription = v.decription,
                   Ord = Convert.ToInt32(a.ord),
                   Active = Convert.ToInt32(v.active),
                   Type = Convert.ToInt32(a.type),
                   Price = Convert.ToDouble(v.Price),
                   FromDate = Convert.ToDateTime(v.FromDate),
                   ToDate = Convert.ToDateTime(v.ToDate),
                   Languagecode = Convert.ToInt32(v.languagecode)
               }).Distinct();
           return lReturn;
       }
       public IQueryable<RelaxAdvertiseDataContract> GetDataItem()
       {
           var lReturn = from a in _context.ADVERTISE_REFs
                         select new RelaxAdvertiseDataContract
                         {
                             Id = Convert.ToInt32(a.id),
                             Ord = Convert.ToInt32(a.ord)
                         };
           return lReturn;
       }
       #endregion
       #region Add
       public void Add(ADVERTISE_REF advertise)
       {
           _context.ADVERTISE_REFs.InsertOnSubmit(advertise);
           _context.SubmitChanges();
          
       }
       #endregion
       #region Update

       public void Update(ADVERTISE_REF advertise)
       {
           var first = _context.ADVERTISE_REFs.FirstOrDefault(a => a.id == advertise.id);
           if (first!=null)
           {
             
               first.ord = advertise.ord;
               first.type = advertise.type;
               _context.SubmitChanges();
           }
       }
       #endregion

       #region Delete
       public void Delete(long iId)
       {
           var first = _context.ADVERTISE_REFs.FirstOrDefault(a => a.id == iId);
           if (first != null)
           {
               _context.ADVERTISE_REFs.DeleteOnSubmit(first);
               _context.SubmitChanges();
           }
       }
       #endregion
   }
}
